package com.zhangchenhui.jmh;

import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.RunnerException;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;

import java.util.concurrent.TimeUnit;

/**
 * @author 张晨辉
 * @Classname JMHExample01
 * @Description TODO
 * @Date 2020/8/11 下午11:00
 */
@OutputTimeUnit(TimeUnit.MILLISECONDS)
@BenchmarkMode(Mode.AverageTime)
@Threads(5)
@Fork(1)
@Measurement(iterations = 5)
@Warmup(iterations = 2)
public class JMHExample06 {
    //Test 的实例将会被多个线程共享 ， 也就是说只有一份 Test 的实例
    @State(Scope.Benchmark)
    public static class test{
        public test(){
           System.out.println("create Instance");
        }

        public void method(){

        }
    }

    @Benchmark
    public void test2(test test) throws InterruptedException {
        test.method();
    }

    public static void main(String[] args) throws RunnerException {
        final Options opts = new OptionsBuilder()
                .include(JMHExample06.class.getSimpleName())
                .build();
        new Runner(opts).run();


    }
}
